package com.ruoyi.collection.process;

import com.alibaba.fastjson.JSON;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.service.ILoanIouService;
import com.ruoyi.collection.domain.ApiParameter;
import com.ruoyi.collection.domain.CollectionRepaymentCalcReq;
import com.ruoyi.collection.domain.CollectionRepaymentCalcResp;
import com.ruoyi.collection.process.api.CollectionProcess;
import com.ruoyi.common.constant.UserConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取实时还款计划列表
 * @author Victor
 *
 */
@Slf4j
@Service("CollectionRepaymentCalc")
public class CollectionRepaymentCalcPorcessor implements CollectionProcess {

    @Autowired
    ILoanIouService loanIouService;

	@Override
	public Object process(ApiParameter req) throws Exception {
		
		log.info("获取实时还款计划列表开始");

		CollectionRepaymentCalcReq reqBody = JSON.parseObject(req.getBody().toString(), CollectionRepaymentCalcReq.class);
		CollectionRepaymentCalcResp respBody = new CollectionRepaymentCalcResp();
		
		Map<String, BigDecimal> realAmtMap = new HashMap<String, BigDecimal>();
	    List<Map<String, Object>> planList = new ArrayList<Map<String, Object>>();
	    respBody.setRealAmtMap(realAmtMap);
	    respBody.setPlanList(planList);
		
		for(String loanSerialNo : reqBody.getContrList()){
			//获取借据详情
	        LoanIou LoanIouQuery = new LoanIou();
	        LoanIouQuery.setLoanSerialNo(loanSerialNo);;
	        LoanIouQuery.setState(UserConstants.BUSINESS_NORMAL);
	        List<LoanIou> loanList = loanIouService.selectLoanIouList(LoanIouQuery);
	        
	        if (loanList.size() != 1) {
	        	log.error("获取借据详情异常{}", loanSerialNo);
	        	throw new Exception("获取借据详情异常");
	        }
	        
	        // 组装应答
	        genLoanResult(loanList.get(0), realAmtMap, planList);
		}
		
		log.info("获取实时还款计划列表结束");
		return respBody;
	}

	private void genLoanResult(LoanIou loanIou, Map<String, BigDecimal> realAmtMap, List<Map<String, Object>> planList) {
		
		// 总应还计算
		BigDecimal totalRec = loanIou.getLoanPrincipal().add(loanIou.getLoanInterest())
				.add(loanIou.getLateFee()).add(loanIou.getServiceFee());
		BigDecimal totalPaid = loanIou.getRepaidPrincipal().add(loanIou.getRepaidInterest())
				.add(loanIou.getRepaidLateFee()).add(loanIou.getRepaidServiceFee());
		BigDecimal totalAdj = loanIou.getFeeRemissionAmt();
		BigDecimal totalOutstd = totalRec.subtract(totalPaid).subtract(totalAdj);
		
		realAmtMap.put(loanIou.getLoanSerialNo(), totalOutstd);
		
		// 生成还款计划，写死1期
		Map<String, Object> plan = new HashMap<String, Object>();
		plan.put("TERM", 1);
		plan.put("CURR_BAL", totalOutstd);
		plan.put("REC_PRINCIPAL", loanIou.getLoanPrincipal());
		plan.put("REC_INT", loanIou.getLoanInterest());
		plan.put("REC_OVERDUE_INT", loanIou.getLateFee());
		plan.put("REC_SERVICE_FEE", loanIou.getServiceFee());
		plan.put("PAID_PRINCIPAL", loanIou.getRepaidPrincipal());
		plan.put("PAID_INT", loanIou.getRepaidInterest());
		plan.put("PAID_OVERDUE_INT", loanIou.getRepaidLateFee());
		plan.put("PAID_SERVICE_FEE", loanIou.getRepaidServiceFee());
		plan.put("OUTSTD_PRINCIPAL", loanIou.getLoanPrincipal().subtract(loanIou.getRepaidPrincipal()));
		plan.put("OUTSTD_INT", loanIou.getLoanInterest().subtract(loanIou.getRepaidInterest()));
		plan.put("OUTSTD_OVERDUE_INT", loanIou.getLateFee().subtract(loanIou.getRepaidLateFee()));
		plan.put("OUTSTD_SERVICE_FEE", loanIou.getServiceFee().subtract(loanIou.getRepaidServiceFee()));
		plan.put("ADJ_TOTAL", totalAdj);

		
		planList.add(plan);
	}

}
